/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.balysv.materialripple.demo.util;

import com.balysv.materialripple.demo.ResourceTable;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

public class CustomToolbar extends DirectionalLayout implements Animator.StateChangedListener, Component.ClickedListener {
    static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "0000252584");
    private static final long TIMER_DRAWER_ANIMATOR = 400;
    private static final long TIMER_DRAWER_ICON_ANIMATOR = 200;
    private Image mMenuImage;
    private ListContainer mMenuListView;
    private PopupDialog mPopupWindow;
    private Text mSpinnerText;
    private Image mTab;
    private Image mDrawerImage;
    private DirectionalLayout mMainLayout;
    private DirectionalLayout mDrawerLayout;
    private ListContainer mDrawerList;
    private Text mTextListView;
    private Text mTextRecyclerView;
    private AnimatorProperty mAnimatorIcon;
    private AnimatorProperty mAnimatorLayout;
    private Boolean selOpen = true;
    private OnSpinnerItemSelectedListener mOnSpinnerItemSelectedListener;
    private OnMenuItemSelectedListener mOnMenuItemSelectedListener;
    private OnAnimatorEndListener mOnAnimatorEndListener;

    public CustomToolbar(Context context) {
        super(context);
        initView(context);
    }

    public CustomToolbar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView(context);
    }

    public CustomToolbar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView(context);
    }

    /**
     * 初始化布局
     *
     * @param context 上下文
     */
    private void initView(Context context) {
        mContext = context;
        Component layout = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ability_toolbar_main, this, true);
        initMenu(context, layout);
    }

    /**
     * 初始化自定义的menu样式
     *
     * @param context 上下文
     * @param layout 布局文件
     */
    private void initMenu(Context context, Component layout) {
        mTextListView = (Text) layout.findComponentById(ResourceTable.Id_text_middle);
        mTextRecyclerView = (Text) layout.findComponentById(ResourceTable.Id_text_left);
        setTextListView(mTextListView);
        setTextRecyclerView(mTextRecyclerView);
    }

    @Override
    public void onStart(Animator animator) {}

    @Override
    public void onStop(Animator animator) {}

    @Override
    public void onCancel(Animator animator) {}

    @Override
    public void onEnd(Animator animator) {
        if (mAnimatorIcon != null && animator == mAnimatorIcon) {
            mAnimatorIcon.reset();
            mAnimatorIcon.resume();
            mAnimatorIcon = null;
            mOnAnimatorEndListener.onEnd(mDrawerImage, selOpen);
        }
    }

    @Override
    public void onPause(Animator animator) {}

    @Override
    public void onResume(Animator animator) {}

    /**
     * 页面点击事件处理
     *
     * @param component 组件
     */
    @Override
    public void onClick(Component component) {
        HiLog.info(label, "onClick id = " + component.getId());
        switch (component.getId()) {

            default:
                break;
        }
    }

    public void startAnimater() {
        if (!selOpen) {
            if (mAnimatorIcon == null) {
                mAnimatorIcon = mDrawerImage.createAnimatorProperty();
            }
            mAnimatorIcon.rotate(180).setDuration(200);
            mAnimatorIcon.setStateChangedListener(this);
            mAnimatorIcon.start();
        }
    }

    public void setSelOpen(Boolean b) {
        selOpen = b;
    }

    /**
     * 注销动画
     */
    public void resetAnimator() {
        if (mAnimatorIcon != null) {
            mAnimatorIcon = null;
        }
        if (mAnimatorLayout != null) {
            mAnimatorLayout = null;
        }
    }

    public Text getTextListView() {
        return mTextListView;
    }

    public void setTextListView(Text textListView) {
        this.mTextListView = textListView;
    }

    public Text getTextRecyclerView() {
        return mTextRecyclerView;
    }

    public void setTextRecyclerView(Text textRecyclerView) {
        this.mTextRecyclerView = textRecyclerView;
    }

    public interface OnSpinnerItemSelectedListener {
        void onSpinnerItemSelected(int position);
        void onSpinnerNothingSelected();
    }

    /**
     * 注册模仿Spinner布局的item监听回调
     *
     * @param listener 监听
     */
    public void setSpinnerOnItemSelectedListener(OnSpinnerItemSelectedListener listener) {
        mOnSpinnerItemSelectedListener = listener;
    }

    public interface OnMenuItemSelectedListener {
        void onMenuItemSelected(int position);
        void onMenuNothingSelected();
    }

    /**
     * 注册模仿Menu布局的item监听回调
     *
     * @param listener 监听
     */
    public void setMenuOnItemSelectedListener(OnMenuItemSelectedListener listener) {
        mOnMenuItemSelectedListener = listener;
    }

    /**
     * 当动画结束时的回调接口定义
     */
    public interface OnAnimatorEndListener {
        void onEnd(Image drawerImage, Boolean selOpen);
    }

    /**
     * 注册动画结束的监听回调
     *
     * @param listener
     */
    public void setOnAnimatorEndListener(OnAnimatorEndListener listener) {
        mOnAnimatorEndListener = listener;
    }
}
